package com.lz.yz.member.web;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.member.entity.Member;
import com.lz.yz.member.model.MemberLableModel;
import com.lz.yz.member.model.MemberModel;
import com.lz.yz.member.model.RequestMemberModel;
import com.lz.yz.member.param.MemberParam;
import com.lz.yz.member.service.AccountService;
import com.lz.yz.member.service.MemberService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* Created by CodeGenerator on 2017/11/09.
*/
@RestController
@RequestMapping("/member")
public class MemberController {
    @Resource
    MemberService memberService;

    @Autowired
    AccountService accountService;

    @Autowired
    HttpServletRequest request;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @ApiOperation(value="获取用户个人信息",response = MemberModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer ")
    })
    @GetMapping("/info")
    public ReturnValue getUserInfo() {
        MemberParam param =new MemberParam();
        param.setAccountId(accountService.getAccountIdByRequest(request));
        return memberService.getUserInfo(param);
    }

    @ApiOperation(value="获取他人用户信息",response = MemberModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer ")
    })
    @GetMapping("/other/{id}")
    public ReturnValue getOther(@PathVariable Integer id) {
        Integer accountId = (Integer) request.getAttribute("accountId");
        MemberParam param =new MemberParam();
        param.setAccountId(id);
        param.setLoginId(accountId);
        return  memberService.getOtherUserInfo(param);
    }

    @ApiOperation(value="修改用户个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "model", value = "用户实体", dataType = "RequestMemberModel", paramType = "body"),
    })
    @PostMapping("/info")
    public ReturnValue update(@RequestBody RequestMemberModel model) throws IllegalAccessException, NoSuchFieldException, InstantiationException {
        try{
            Integer accountId = (Integer) request.getAttribute("accountId");
            Integer memberId = memberService.findBy("accountId",accountId).getId();
            model.setMemberId(memberId);
            model.setAccountId(accountId);
            memberService.updateUser(model);
            return ReturnValue.generateTrueMsgReturnValue("更改个人信息成功!");
        }catch (Exception e){
            logger.error("更改个人信息异常", e);
            return ReturnValue.generateFalseReturnValue("更改个人信息异常!");
        }
    }
}
